package sample.server;

import java.net.URL;
import java.util.Properties;

import sample.shared.Consts;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.name.Names;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.servlet.ServletModule;

public class BootstrapListener extends GuiceServletContextListener
{
  @Override
  protected Injector getInjector() {
    return Guice.createInjector(new ServeUrlModule());
  }

  public static class ServeUrlModule extends ServletModule
  {
    @Override
    protected void configureServlets() {
      //requestStaticInjection(JDOSession.class);

      try {
        Properties props = loadProperties();
        Names.bindProperties(binder(), props);
      }
      catch (Exception e) {
        e.printStackTrace();
      }

      serve("*goto").with(GotoServlet.class);
      serve("*gupld").with(UploadServlet.class);
      serve("*edit").with(EditServlet.class);      
      serve("*clientmanager").with(ClientManagerServiceImpl.class);
      
      Consts.NEWLINE = System.getProperty("line.separator");
    }

    private Properties loadProperties() throws Exception {
      Properties properties = new Properties();
      URL url = BootstrapListener.class.getResource("props");
      properties.load(url.openStream());
      return properties;
    }

  }

}
